package com.lyw;

import com.lyw.constants.GlobalVariables;
import com.lyw.domain.GlobalConfigurationInfo;
import com.lyw.enums.*;
import com.lyw.exception.GlobalException;
import com.lyw.factory.TimerTaskFactory;
import com.lyw.handle.*;
import com.lyw.utils.*;
import com.sun.jna.platform.win32.WinDef;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.lyw.constants.CommonConstants.WINDOW_HEIGHT;

/**
 * 主页面
 */
@Component
public class Window extends JFrame {

  @Autowired
  private TimerTaskFactory taskFactory;

  {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  }

  @PostConstruct
  public void init() throws Exception {
    GlobalConfigurationInfo configInfo = loadConfiguration();
    final Robot robot;
    try {
      robot = new Robot();
    } catch (AWTException e) {
      throw new GlobalException("创建机器人失败！");
    }
    Rectangle rectangle = prepareConfigInfo(configInfo, robot);
    configInfo.setCurCharacter(CharacterEnum.NAN_GUI_SWORD_SAINT);

   /* Mat door = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\TPlan\\TPlan\\src\\main\\resources\\images\\other\\door.png");
    DataFilteringInterface filter = new TopProcessor();
    int[] positions = {-1, -1, -1, -1};
    while (true) {
      Mat mat = OpenCvUtil.bufferedImageToMat(robot.createScreenCapture(rectangle));
      filter.findTheTargetDoor(robot, mat, door, positions);
      System.out.println();
    }*/
    /*while (true) {
      int[] cur_characters_position_on_the_mini_map = SmallMapUtil.getTheMiniMapWhereTheCharacterIsLocated(robot, DuplicateEnum.JI_JING_CITY);

      // 下一个房间坐标位置
      int[] nextRoomPosition = DuplicateEnum.JI_JING_CITY.getNextRoomPosition(cur_characters_position_on_the_mini_map);
      if (nextRoomPosition[0] == -1) {
        return;
      }
      System.out.println(cur_characters_position_on_the_mini_map[0] + ", " + cur_characters_position_on_the_mini_map[1]);
      System.out.println(nextRoomPosition[0] + ", " + nextRoomPosition[1]);
      if (cur_characters_position_on_the_mini_map[0] == 1 && cur_characters_position_on_the_mini_map[1] == 3) {
        FileUtils.clearClearancePosition();
      }
      FileUtils.writeClearanceRoomPosition(cur_characters_position_on_the_mini_map);
      robot.delay(5000);
//      OpenCvUtil.matchEquipmentGetSimplePosition(taskFactory, robot, rectangle, 90);
//      OpenCvUtil.matchMiniMapWhetherClearance(robot, 18);
    }*/

    /*Mat mat = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\11.png");
    int[] positions = {-1, -1, -1, -1};
    OpenCvUtil.matchMonsterExceptionTemplate(startPosition,
            mat,
            positions);*/
    DuplicateUtil.openDungeon(taskFactory, robot, rectangle, configInfo, DuplicateEnum.JI_JING_CITY);
    /*while (true) {
      CharacterUtil.switchCharacter(configInfo, robot, rectangle);
      // 移动到西海岸页面
      CharacterUtil.moveCharacterToDestinationByWestCoast(configInfo, robot, rectangle.getLocation());
      // 进入寂静城
      CharacterUtil.clickMap(robot, location,  MapPosition.JI_JING_CITY, KeyEvent.VK_RIGHT);
      // TODO 处理图标
      CharacterUtil.handleIcon(configInfo, robot, rectangle.getLocation());
      // 移动到副本
      MouseAndKeyEventUtil.moveToDuplicate(robot, rectangle.getLocation(), DuplicateEnum.JI_JING_CITY, KeyEvent.VK_RIGHT);
      // 刷副本
      DuplicateUtil.openDungeon(taskFactory, robot, rectangle, configInfo, DuplicateEnum.JI_JING_CITY);
      // TODO 当前行代表角色的疲劳刷完了，徐娅 esc 选择角色
    }*/


    /*
      TODO 对角线移动
      宽176-47 = 130 = 16641 26
      高513-428 = 85 = 7225 17
      对角线 = 155 = 23866 31
      26：17：31
    */
  }

  private Rectangle prepareConfigInfo(GlobalConfigurationInfo configInfo, Robot robot) {

    final WinDef.RECT windowPic = WindowUtil.getWindowPic(configInfo);
    Rectangle rectangle = windowPic.toRectangle();
    SmallMapUtil.prepareMapRectangle(rectangle);
    SkillUtil.prepareEnableSkillScan(rectangle);
    MouseAndKeyEventUtil.prepareMouseAndKeyboardTool(robot, taskFactory);
    OpenCvUtil.preparePng();
    OpenCvUtil.prepareCharacterArea(rectangle);
    DuplicateEnum.load_mini_map();
    CharacterEnum.load_character_skill();
    return rectangle;
  }

  private GlobalConfigurationInfo loadConfiguration() {

    List<String> fieldNames = ReflectUtil.getClassFieldNames(GlobalConfigurationInfo.class);
    return (GlobalConfigurationInfo) GlobalConfigUtil.setClassValueByFieldNames(fieldNames, GlobalConfigurationInfo.class);
  }
}
